package com.zq.zqplayer.mvp.live.ui

import android.hardware.Camera
import android.os.Bundle
import android.view.SurfaceHolder
import android.widget.Toast
import com.ziq.base.base.BaseActivity
import com.ziq.base.extension.binding
import com.ziq.base.extension.click
import com.ziq.base.utils.CameraUtils
import com.zq.playerlib.ZQPusher
import com.zq.zqplayer.common.Constants
import com.zq.zqplayer.databinding.ActivityPushTheFlowBinding

class PushTheFlowActivity : BaseActivity(), SurfaceHolder.Callback, Camera.PreviewCallback {

    private val binding : ActivityPushTheFlowBinding by binding()

    var pusher : ZQPusher = ZQPusher()
    var isPushed:Boolean = false;

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding.surfaceView.holder.addCallback(this)
        binding.surfaceView.setOnTouchListener { v, event ->
            try {
                CameraUtils.doFocus(event) { success, camera -> Toast.makeText(this@PushTheFlowActivity, "onAutoFocus:\n$success", Toast.LENGTH_LONG).show() }
            } catch (e: Exception) {
                e.printStackTrace()
            }
            false
        }
        binding.btnPush.click {
            isPushed = true
        }
    }



    override fun surfaceCreated(holder: SurfaceHolder) {
        try {
            CameraUtils.openCamera(this@PushTheFlowActivity, false, binding.surfaceView.getWidth(), binding.surfaceView.getHeight())
            CameraUtils.setCameraDisplayOrientation(this@PushTheFlowActivity)
            CameraUtils.startPreviewDisplay(holder, this@PushTheFlowActivity)

//            pusher.initVideo("rtmp://193.112.65.251:1935/stream/ziq", mSurfaceView.getWidth(), mSurfaceView.getHeight())
            val result:Int = pusher.initVideo(Constants.pushUrl,
                    CameraUtils.getCamera().getParameters().getPreviewSize().width,
                    CameraUtils.getCamera().getParameters().getPreviewSize().height)
            if(result == 0){

            }
        } catch (e: Exception) {
            e.printStackTrace()
        }

    }

    override fun surfaceChanged(holder: SurfaceHolder, format: Int, width: Int, height: Int) {
        try {
            CameraUtils.stopPreview()
            CameraUtils.setCameraDisplayOrientation(this@PushTheFlowActivity)
            CameraUtils.startPreviewDisplay(holder, this@PushTheFlowActivity)
        } catch (e: Exception) {
            e.printStackTrace()
        }

    }

    override fun surfaceDestroyed(holder: SurfaceHolder) {
        try {
            CameraUtils.stopPreview()
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    override fun onPreviewFrame(data: ByteArray?, camera: Camera?) {
        //Android camera preview默认格式为NV21的, 要 转为 I420（即YUV标准格式4：2：0）再进行下一步
        if(isPushed){
            pusher.onFrameCallback(data)
        }
    }

    override fun onDestroy() {
        try {
            CameraUtils.stopPreview()
            CameraUtils.releaseCamera()
        } catch (e: Exception) {
        }
        super.onDestroy()
    }

}